﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RpcGenerator.CSharp;

namespace RpcGenerator
{
    class FileGenerator
    {
        public static void GenerateShareMessage(MessageFieldDescription description, string path,MessageFormatType format)
        {
            DefaultMessageSerializer serialize = new DefaultMessageSerializer(0);
            ShareFieldGenerator sgenerator = new ShareFieldGenerator();
            NamespaceGenerator ngenerator = new NamespaceGenerator();
            MessageNamespace Namespace = ngenerator.Generate(description,NamespaceType.CSharp);

            int lineCount = 0;
            Namespace.ReferenceNamespace.ForEach(reference =>
            {
                if (lineCount++ == 0)
                {
                    serialize.Apppend(string.Format("using {0};", reference));
                }
                else
                {
                    serialize.NewLine(string.Format("using {0};", reference));
                }
            });
            serialize.NewLine();
            serialize.NewLine(string.Format("namespace {0}", Namespace.Namespace));
            serialize.BeginBlock();
            sgenerator.Generate(description, serialize, format);
            serialize.EndBlock();

            string fileName = string.Format("./{0}Extension.cs", description.Type.Name);
            fileName = Path.Combine(path, fileName);
            File.WriteAllText(fileName, serialize.ToString(), Encoding.Unicode);
        }

        public static void GenerateMessageInterfaceProxyAndInvoker(Type type, List<MessageDescription> allDescriptions, string path,MessageFormatType format)
        {
            DefaultMessageSerializer serializer = new DefaultMessageSerializer(0);

            NamespaceGenerator ngenerator = new NamespaceGenerator();
            MessageNamespace Namespace = ngenerator.Generate(type, allDescriptions,NamespaceType.CSharp);
            int lineCount = 0;
            Namespace.ReferenceNamespace.ForEach(reference =>
            {
                if (lineCount++ == 0)
                {
                    serializer.Apppend(string.Format("using {0};", reference));
                }
                else
                {
                    serializer.NewLine(string.Format("using {0};", reference));
                }
            });

            string s = serializer.ToString();

            serializer.NewLine();
            serializer.NewLine(string.Format("namespace {0}", Namespace.Namespace));
            serializer.BeginBlock();

            InterfaceGenerator.Generate(type, serializer, allDescriptions);
            serializer.NewLine();
            ProxyGenerator.Generate(type, serializer, allDescriptions);
            serializer.NewLine();
            serializer.NewLine("#region internal");
            InvokerGenerator.Generate(type, serializer, allDescriptions,format);
            allDescriptions.ForEach(description =>
            {
                MessageGenerator mgenerator = new MessageGenerator();
                serializer.NewLine();
                mgenerator.Generate(description, serializer,format);
            });
            serializer.NewLine();
            AsynCallbackGenerator.Generate(serializer, allDescriptions);
            serializer.NewLine("#endregion");

            serializer.EndBlock();
            string fileName = string.Format("./{0}_MIPI.cs", type.Name);
            fileName = Path.Combine(path, fileName);
            File.WriteAllText(fileName, serializer.ToString(), Encoding.Unicode);
        }
    }
}
